# -*- coding: utf-8 -*-
from django import forms
from django.db import models
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from store.widgets import WYMEditor

from store.models import Category,Card,CardNumber,CardOrder

class CardNumberForm(forms.ModelForm):
    def __init__(self,*args,**kwargs):
        super(CardNumberForm,self).__init__(*args,**kwargs)
        
    class Meta:
        model = CardNumber
        fields = ('card',)
    numbers = forms.CharField(label=u'卡号',widget=forms.Textarea,help_text=u"两张卡之间用换行分隔")
        
class CardNumberInline(admin.StackedInline):
    template = 'admin/store/edit_inline.html'
    model = CardNumber
    extra = 0
    fields = ("number",)
    readonly_fields = ("number",)
    can_delete = False
    verbose_name = _(u'充值卡信息')

class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug','status','sort_index','status')
    search_fields = ('name',)
    list_filter = ['status',]
    
class CardAdmin(admin.ModelAdmin):
    list_display = ('name', 'price','img','sort_index','status')
    search_fields = ('name',)
    list_filter = ['status',]
    list_editable = ('sort_index',)
    formfield_overrides = {
          models.TextField: {'widget': WYMEditor},
        }
#    inlines = [CardNumberInline,]

class CardNumberAdmin(admin.ModelAdmin):
    list_display = ('number','card', 'status','create_on')
    search_fields = ('number',)
    fields = ('card','number')
    add_fieldsets = (
        (None, {
            'fields': ('card', 'numbers',)}
        ),
    )
    add_form = CardNumberForm
    list_filter = ('card','status',)

    def get_fieldsets(self, request, obj=None):
        if not obj:
            return self.add_fieldsets
        return super(CardNumberAdmin, self).get_fieldsets(request, obj)
    
    def get_form(self, request, obj=None, **kwargs):
        defaults = {}
        if obj is None:
            defaults.update({
                'form': self.add_form,
                'fields': admin.util.flatten_fieldsets(self.add_fieldsets),
            })
        defaults.update(kwargs)
        return super(CardNumberAdmin, self).get_form(request, obj, **defaults)
    
    def __init__(self, model, admin_site):
        from django import forms
        print self.form
        super(CardNumberAdmin, self).__init__(model,admin_site)
    
    def save_model(self, request, obj, form, change):
        if not change:
            numbers = request.POST["numbers"].split('\n')
            for number in numbers:
                obj = self.save_form(request, form, change=False)
                obj.number = number
                obj.create_by = request.user
                obj.save()
                form.instance.pk = None
        else:
            obj.save()
        
class CardOrderAdmin(admin.ModelAdmin):
    list_display = ('code','product_name', 'product_quantity','product_price','total_amount','create_on','create_ip','status')
    search_fields = ('code','product_name')
    fieldsets = (
        (_(u'订单信息'), {'fields': ('code', 'status','total_amount','user','create_on','create_ip')}),
        (_(u'商品信息'), {'fields': ('product_name','product_quantity','product_price')}),
    )
    readonly_fields = ('code','status','total_amount','create_on','create_ip','user','product_name','product_quantity','product_price')

    list_filter = ('card','status','create_on',)
    inlines = [CardNumberInline,]
    
admin.site.register(Category, CategoryAdmin)
admin.site.register(Card, CardAdmin)
admin.site.register(CardNumber, CardNumberAdmin)
admin.site.register(CardOrder, CardOrderAdmin)
